<?php
// +----------------------------------------------------------------------
// | 部门控制器
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\service\SysDeptService;
use app\common\enum\ErrorEnum;
use think\facade\View;
use app\common\enum\BaseStatusEnum;
class SysDept extends AdminController
{
    public function index(){
        $list = SysDeptService::list();
        $data = [
            'list' => flattenMenuArray($list),
            'statusList' => BaseStatusEnum::getAll()
        ];

        return View::fetch('', $data);
    }

    public function add(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'dept_name' => 'require',
        ];
        //提示信息
        $message = [
            'dept_name.require' => '部门名不能为空!',
        ];
        $this->validate($data, $validate, $message);
        return success(SysDeptService::save($data));
    }

     //更新
     public function edit(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id' => 'require',
            'dept_name' => 'require',
        ];
        //提示信息
        $message = [
            'id.require' => 'ID不能为空!',
            'dept_name.require' => '部门名不能为空!',
        ];

        $this->validate($data, $validate, $message);

        return success(SysDeptService::update($data));
    }

    // 详情
    public function info() {
        $id = $this->request->get("id");
        if (empty($id)) {
            return success();
        }
        return success(SysDeptService::info($id));
    }

    //删除
    public function delete(){
        $ids = $this->request->get("ids");
        if(empty($ids)){
            return failure(ErrorEnum::SELECT_DELETE_DATA_CODE, ErrorEnum::SELECT_DELETE_DATA_DESC);
        }

        SysDeptService::destroy($ids);
        return success();
    }


}
